/* 03_MERGE.do                 KTS/DCC/NLB                 yyyy-mm-dd:2026-02-04
----|----1----|----2----|----3----|----4----|----5----|----6----|----7----|----8

This do file creates the data used in the paper "Estimating Inter­generational
Returns to Medical Care: New Evidence from At­Risk Newborns" written by
Damian Clarke, Nicolas Lillo Bustos and Kathya Tapia-Schythe.  
In certain cases these results will require the user-written, labutil,
personage commands. 
*/

clear all 

* Preamble:
clear all
set more off
timer on 1

* Switch to destination directory:
cd "$dtadir/DEIS"

*-------------------------------------------------------------------------------
*--- 1. Loading NAC data: Merge with NAC NEWVARS 
*-------------------------------------------------------------------------------

* Load birth data:
use "${nac_original}_NOGLOSAS_NODUPS_NONAS.dta", clear

* Merge in mother birth data:
merge m:1 ID_MADRE using MOTHER_BIRTHDATA.dta, gen(mrg_mbdata2NAC) keep(master match)
label var mrg_mbdata2NAC "merge m:1 ID_MADRE using MOTHER_BIRTHDATA.dta"

* Merge in fertility variables:
merge 1:1 ID_RECIEN_NACIDO using FERTILITY_VARIABLES.dta, gen(mrg_frt2NAC)
label var mrg_frt2NAC "merge 1:1 ID_RECIEN_NACIDO using FERTILITY_VARIABLES.dta"

* Merge in birth spacing variables:
merge 1:1 ID_RECIEN_NACIDO using BIRTH_SPACING.dta, gen(mrg_bspcng2NAC)
label var mrg_bspcng2NAC "merge 1:1 ID_RECIEN_NACIDO using BIRTH_SPACING.dta"

* Merge in SGA variables:
merge 1:1 ID_RECIEN_NACIDO using SGA_VARIABLES.dta, nogen

* Merge in education variables:
merge 1:1 ID_RECIEN_NACIDO using EDUCATION_VARIABLES.dta, nogen

* Merge in heaping variables:
merge 1:1 ID_RECIEN_NACIDO using HEAPING_VARIABLES.dta, nogen

* Merge in low birth weight indicators:
merge 1:1 ID_RECIEN_NACIDO using LBW_INDICATORS.dta, nogen

* Merge in birth timing variables:
merge 1:1 ID_RECIEN_NACIDO using BIRTH_TIMING_VARIABLES.dta, nogen

* Merge in mortality variables:
merge 1:1 ID_RECIEN_NACIDO using MORTALITY_VARIABLES.dta, nogen

* Merge in activity variables:
merge 1:1 ID_RECIEN_NACIDO using ACTIVITY_VARIABLES.dta, nogen

* Merge in income variables (child):
merge 1:1 ID_RECIEN_NACIDO using INCOME_VARIABLES.dta, nogen

* Merge in income transition variables of the parents assigned to each child:
merge 1:1 ID_RECIEN_NACIDO using INCOME_TRANSITION_VARIABLES.dta, nogen //not found

* Merge in income variables (mother):
merge m:1 ID_MADRE using MOTHER_INCOME_DATA.dta, gen(mrg_myd2NAC) keep(master match)
label var mrg_myd2NAC "merge m:1 ID_MADRE using MOTHER_INCOME_DATA.dta"

* Merge in mother's isapre status before and after each birth:
merge m:1 ID_MADRE FECHA_NACIMIENTO_SIF using ISAPRECLINIC_MOTHERS.dta, gen(mrg_isaprem2NAC)
label var mrg_isaprem2NAC "merge m:1 ID_MADRE FECHA_NACIMIENTO_SIF using ISAPRE_MOTHERS.dta"

* Merge in BLN (2013) control variables:
merge 1:1 ID_RECIEN_NACIDO using BLN2013_CONTROLS.dta, nogen

*-------------------------------------------------------------------------------
*--- 2. Merge with EEHH NEWVARS 
*-------------------------------------------------------------------------------

* ALL TYPES OF HOSPITAL STAYS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_ALL.dta, nogen ///
	keepusing(mrg_EEHH2NAC days_y?? nadmssn_y?? ndischrg_y?? total_discharges)

* HOSPITAL STAYS IN PRIVATE CLINICS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_PRIVATE.dta, nogen ///
	keepusing(*_priv)

* HOSPITAL STAYS IN PUBLIC HOSPITALS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_PUBLIC.dta, nogen ///
	keepusing(*_pblc)

* HOSPITAL STAYS COVERED BY PRIVATE INSURANCE (ISAPRE)	
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_ISAPRE.dta, nogen ///
	keepusing(*_ispr)

* HOSPITAL STAYS RELATED TO ABORTIONS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_ABORTIONS.dta, nogen ///
	keepusing(*_abrtn)

* HOSPITAL STAYS RELATED TO BIRTH/PREGNANCY CODES:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_BCODES.dta, nogen ///
	keepusing(*_bcodes)	

* HOSPITAL STAYS RELATED TO REPRODUCTIVE HEALTH CONDITIONS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_REPHEALTH.dta, nogen ///
	keepusing(*_rphlth)	

* HOSPITAL STAYS RELATED TO NOT EXTERNAL CONDITIONS:
merge 1:1 ID_RECIEN_NACIDO using DAYS_Y_AGG_NOTEXT.dta, nogen ///
	keepusing(*_notext)		

*-------------------------------------------------------------------------------
*--- 3. Final export 
*-------------------------------------------------------------------------------	
	
* Save dataset:
compress
label data "Working dataset $S_DATE"
notes drop _dta
note: Last modification timestamp: $S_DATE at $S_TIME
save "workingdata.dta", replace

* Close log
timer off 1
timer list 1
